package com.sky.controller.admin;

import com.sky.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;

@Api(tags = "店铺相关接口")
@Slf4j
@RequestMapping("/admin/shop")
@RestController("adminShopController")
public class ShopController {
    public static final String SHOP_STATUS = "SHOP_STATUS";

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 设置营业状态
     * @param status
     * @return
     */
    @ApiOperation("设置营业状态")
    @PutMapping("/{status}")
    public Result setStatus(@PathVariable Integer status){
        log.info("设置营业状态：{}", status==1 ? "营业中" : "打烊中");
        //1.调用redisTemplate，获取valueOpreations对象
        ValueOperations valueOperations = redisTemplate.opsForValue();

        //2.通过valueOpreations对象，设置状态数据, 1代表营业中， 0代表打烊中
        valueOperations.set(SHOP_STATUS, status);

        return Result.success();
    }

    /**
     * 获取营业状态
     * @return
     */
    @ApiOperation("获取营业状态")
    @GetMapping("/status")
    public Result getStatus(){
        //1.调用redisTemplate，获取valueOpreations对象
        ValueOperations valueOperations = redisTemplate.opsForValue();

        //2.通过valueOpreations对象，取出状态key对应的值
        Object status = valueOperations.get(SHOP_STATUS);
        log.info("获取营业状态...{}", status);
        return Result.success(status);
    }

}
